package com.nami.dispatcher.application.convertor;

import com.nami.dispatcher.domain.model.NtfGrpReceiverModel;
import com.nami.dispatcher.facade.grpmsg.request.command.GrpDispatchCmd;
import com.nami.gate.external.basic.bo.ConnectedUserBO;
import com.nami.gate.external.basic.cache.user.UserSessionRel;

import com.nami.im.network.model.message.v1.MessageV1;
import com.nami.im.network.model.message.MsgUser;
import com.nami.im.network.model.message.v2.Message;
import org.dromara.common.core.enums.message.MessageFlowEnum;

import java.util.List;
import java.util.Map;

public class Cmd2GrpModelConvertor {



    private static MessageV1 convertToDownMsgModel(MessageV1 message) {
        MessageV1 msgModel = new MessageV1();
        msgModel.setId(message.getId());
        msgModel.setClientMsgId(message.getClientMsgId());
        msgModel.setCid(message.getCid());
        msgModel.setConversationType(message.getConversationType());
        msgModel.setFlow(MessageFlowEnum.DOWN_MSG.getCode());
        msgModel.setIsCardMsg(message.getIsCardMsg());
        msgModel.setAtUserList(message.getAtUserList());
        msgModel.setCardBody(message.getCardBody());
        msgModel.setCardTemplateId(message.getCardTemplateId());
        msgModel.setCloudCustomData(message.getCloudCustomData());
        msgModel.setIsPeerRead(message.getIsPeerRead());
        msgModel.setPayload(message.getPayload());
        msgModel.setPayloadType(message.getPayloadType());
        msgModel.setNeedReadReceipt(message.getNeedReadReceipt());
        msgModel.setType(message.getType());
        msgModel.setSeq(message.getSeq());
        msgModel.setSenderId(message.getSenderId());
        msgModel.setReceiverId(message.getReceiverId());
        msgModel.setRevoker(message.getRevoker());
        msgModel.setTime(message.getTime());
        msgModel.setOfflinePushInfo(message.getOfflinePushInfo());
        msgModel.setReadReceiptInfo(message.getReadReceiptInfo());

        MsgUser senderInfo = message.getSenderInfo();
        if (senderInfo != null) {
            MsgUser user = new MsgUser();
            user.setNick(senderInfo.getNick());
            user.setAvatar(senderInfo.getAvatar());
            msgModel.setSenderInfo(user);
        }
        return msgModel;
    }

    public static NtfGrpReceiverModel toReceiverTransModel(Map<String, List<UserSessionRel>> map, GrpDispatchCmd cmd) {
        NtfGrpReceiverModel grpModel = new NtfGrpReceiverModel();

        ConnectedUserBO sender = cmd.getSender();
        Message message = cmd.getMessage();

        ConnectedUserBO senderUser = new ConnectedUserBO();
        senderUser.setUserId(sender.getUserId());
        senderUser.setAppKey(sender.getAppKey());
        senderUser.setNick(sender.getNick());
        senderUser.setAvatar(sender.getAvatar());

        grpModel.setSender(senderUser);
        grpModel.setTargetId(message.getTargetId());
        grpModel.setGrpClients(map);
        grpModel.setQos(cmd.getQos());

        grpModel.setMessage(message);
        return grpModel;
    }


}
